/**
 * Created With IntelliJ IDEA
 * Description:牛客网：QQ2 微信红包
 * <a href="https://www.nowcoder.com/practice/fbcf95ed620f42a88be24eb2cd57ec54?tpId=167&tqId=34050&ru=/exam/oj">...</a>
 * User: DELL
 * Data: 2023-04-14
 * Time: 23:41
 */

public class Gift {
    public int getValue(int[] gifts, int n) {
        int temp = gifts[0];
        int num = 0;
        for (int i = 1; i < n; i++) {
            if (gifts[i] == temp) {
                num++;
            } else if (num == 0) {
                temp = gifts[i];
                num = 1;
            } else {
                num--;
            }
        }
        //验证一下 temp 是否合理
        num = 0;
        for (int i = 0; i < n; i++) {
            if (gifts[i] == temp) {
                num++;
            }
        }
        if (num > n / 2) {
            return temp;
        } else {
            return 0;
        }
    }
}